/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dominio;

import java.util.regex.*;
import java.util.StringTokenizer;

/**
 *
 * @author Juan Manuel Fuentes Diaz
 */
public class ParserHttp {

    private StringBuffer titulo;
    private StringBuffer texto;

    private void crearParseHttp() {
        this.titulo = new StringBuffer("");
        this.texto = new StringBuffer("");
    }

    private void contieneTitulo(StringBuffer str) {
        if ((str.indexOf("<title>") != -1) && (str.indexOf("</title>") != -1)) {
            String s1 = str.substring(str.indexOf("<title>") + 7, str.indexOf("</title>"));
            s1 = tokenizarCarext(s1);
            //s1 = new String(tokenizarSig(s1));
            this.titulo.append(s1);
        }
    }

    private void contieneTextoH1(StringBuffer str) {

        if ((str.indexOf("<h1>") != -1) && (str.indexOf("</h1>") != -1)) {
            String s1 = str.substring(str.indexOf("<h1>") + 4, str.indexOf("</h1>"));
            StringBuffer aux1 = new StringBuffer(s1);
            aux1 = new StringBuffer(contieneBasuraScript(aux1));
            aux1 = new StringBuffer(contieneBasuraStyle(aux1));
            aux1 = new StringBuffer(contieneBasuraA(aux1));
            aux1 = new StringBuffer(contieneBasuraLabel(aux1));
            aux1 = new StringBuffer(contieneBasuraDiv(aux1));
            aux1 = new StringBuffer(contieneBasuraFinal(aux1));
            aux1 = new StringBuffer(contieneCopyright(aux1));
            aux1 = new StringBuffer(quitaTabulador(aux1.toString()));
            aux1 = new StringBuffer(quitaEspacios(aux1.toString()));
            s1 = tokenizarCarext(aux1.toString());
            this.texto.append(tokenizarSig(s1));
            this.texto.append(".");
        }
    }

    private void contieneTextoH2(StringBuffer str) {
        if ((str.indexOf("<h2>") != -1) && (str.indexOf("</h2>") != -1)) {
            String s1 = str.substring(str.indexOf("<h2>") + 4, str.indexOf("</h2>"));
            StringBuffer aux1 = new StringBuffer(s1);
            aux1 = new StringBuffer(contieneBasuraScript(aux1));
            aux1 = new StringBuffer(contieneBasuraStyle(aux1));
            aux1 = new StringBuffer(contieneBasuraA(aux1));
            aux1 = new StringBuffer(contieneBasuraLabel(aux1));
            aux1 = new StringBuffer(contieneBasuraDiv(aux1));
            aux1 = new StringBuffer(contieneBasuraFinal(aux1));
            aux1 = new StringBuffer(contieneCopyright(aux1));
            aux1 = new StringBuffer(quitaTabulador(aux1.toString()));
            aux1 = new StringBuffer(quitaEspacios(aux1.toString()));
            s1 = tokenizarCarext(aux1.toString());
            this.texto.append(tokenizarSig(s1));
            this.texto.append(".");
        }
    }

    private void contieneTextoH3(StringBuffer str) {
        if ((str.indexOf("<h3>") != -1) && (str.indexOf("</h3>") != -1)) {
            String s1 = str.substring(str.indexOf("<h3>") + 4, str.indexOf("</h3>"));
            StringBuffer aux1 = new StringBuffer(s1);
            aux1 = new StringBuffer(contieneBasuraScript(aux1));
            aux1 = new StringBuffer(contieneBasuraStyle(aux1));
            aux1 = new StringBuffer(contieneBasuraA(aux1));
            aux1 = new StringBuffer(contieneBasuraLabel(aux1));
            aux1 = new StringBuffer(contieneBasuraDiv(aux1));
            aux1 = new StringBuffer(contieneBasuraFinal(aux1));
            aux1 = new StringBuffer(contieneCopyright(aux1));
            aux1 = new StringBuffer(quitaTabulador(aux1.toString()));
            aux1 = new StringBuffer(quitaEspacios(aux1.toString()));
            s1 = tokenizarCarext(aux1.toString());
            this.texto.append(tokenizarSig(s1));
            this.texto.append(".");
        }
    }

    private void contieneTextoP(StringBuffer str) {
        StringBuffer aux1 = new StringBuffer("");
        StringBuffer aux2 = new StringBuffer(str);
        StringTokenizer st;
        int a = aux2.indexOf("<p>");
        int b = aux2.indexOf("</p>",a);
        while ((a != -1) && (b != -1)) {
            if (a < b) {
                aux1.append(aux2.substring(a + 3, b));
                aux2 = new StringBuffer(aux2.replace(a, b + 4, "."));
                aux1 = new StringBuffer(contieneBasuraScript(aux1));
                aux1 = new StringBuffer(contieneBasuraStyle(aux1));
                aux1 = new StringBuffer(contieneBasuraA(aux1));
                aux1 = new StringBuffer(contieneBasuraLabel(aux1));
                aux1 = new StringBuffer(contieneBasuraDiv(aux1));
                aux1 = new StringBuffer(contieneBasuraFinal(aux1));
                aux1 = new StringBuffer(contieneCopyright(aux1));
                aux1 = new StringBuffer(quitaTabulador(aux1.toString()));
                aux1 = new StringBuffer(quitaEspacios(aux1.toString()));
                String s1 = tokenizarCarext(aux1.toString());
                s1 = tokenizarSig(s1);
                st = new StringTokenizer(s1);
                if (st.countTokens() > 13) {
                    this.texto.append(s1);
                    this.texto.append(".");
                }
                aux1 = new StringBuffer("");
            } else {
                aux2 = new StringBuffer(aux2.replace(a, a + 3, ""));
            }
            a = aux2.indexOf("<p>");
            b = aux2.indexOf("</p>",a);
        }

    }

    private void contieneTextoDiv(StringBuffer str) {
        StringBuffer aux1 = new StringBuffer("");
        StringBuffer aux2 = new StringBuffer(str);
        StringTokenizer st;
        int a = aux2.indexOf("<div class");
        int ab = aux2.indexOf(">",a);
        int b = aux2.indexOf("</div>",a);
        while ((a != -1) && (b != -1)) {
            if (a < b) {
                aux1 = new StringBuffer(aux2.substring(ab+1, b));
                aux2 = new StringBuffer(aux2.replace(a, b + 6, "."));
                aux1 = new StringBuffer(contieneBasuraScript(aux1));
                aux1 = new StringBuffer(contieneBasuraStyle(aux1));
                aux1 = new StringBuffer(contieneBasuraA(aux1));
                aux1 = new StringBuffer(contieneBasuraLabel(aux1));
                aux1 = new StringBuffer(contieneBasuraDiv(aux1));
                aux1 = new StringBuffer(contieneBasuraFinal(aux1));
                aux1 = new StringBuffer(contieneCopyright(aux1));
                aux1 = new StringBuffer(quitaTabulador(aux1.toString()));
                aux1 = new StringBuffer(quitaEspacios(aux1.toString()));
                String s1 = tokenizarCarext(aux1.toString());
                s1 = tokenizarSig(s1);
                st = new StringTokenizer(s1);
                if (st.countTokens() > 13) {
                    this.texto.append(s1);
                    this.texto.append(".");
                }
                aux1 = new StringBuffer("");
            } else {
                aux2 = new StringBuffer(aux2.replace(a, ab+1, ""));
            }
            a = aux2.indexOf("<div class");
            ab = aux2.indexOf(">",a);
            b = aux2.indexOf("</div>",a);
        }

    }
    private void contieneTextoSpan(StringBuffer str) {
        StringBuffer aux1 = new StringBuffer("");
        StringBuffer aux2 = new StringBuffer(str);
        StringTokenizer st;
        int a = aux2.indexOf("<span class");
        int ab = aux2.indexOf(">",a);
        int b = aux2.indexOf("</span>",a);
        while ((a != -1) && (b != -1)) {
            if (a < b) {
                aux1 = new StringBuffer(aux2.substring(ab+1, b));
                aux2 = new StringBuffer(aux2.replace(a, b + 7, "."));
                aux1 = new StringBuffer(contieneBasuraScript(aux1));
                aux1 = new StringBuffer(contieneBasuraStyle(aux1));
                aux1 = new StringBuffer(contieneBasuraA(aux1));
                aux1 = new StringBuffer(contieneBasuraLabel(aux1));
                aux1 = new StringBuffer(contieneBasuraDiv(aux1));
                aux1 = new StringBuffer(contieneBasuraFinal(aux1));
                aux1 = new StringBuffer(contieneCopyright(aux1));
                aux1 = new StringBuffer(quitaTabulador(aux1.toString()));
                aux1 = new StringBuffer(quitaEspacios(aux1.toString()));
                String s1 = tokenizarCarext(aux1.toString());
                s1 = tokenizarSig(s1);
                st = new StringTokenizer(s1);
                if (st.countTokens() > 13) {
                    this.texto.append(s1);
                    this.texto.append(".http://uk.reuters.com/article/domesticNews/idUKTRE4B336B20081204");
                }
                aux1 = new StringBuffer("");
            } else {
                aux2 = new StringBuffer(aux2.replace(a, ab+1, ""));
            }
            a = aux2.indexOf("<span class");
            ab = aux2.indexOf(">",a);
            b = aux2.indexOf("</span>",a);
        }

    }

    private StringBuffer contieneBasuraScript(StringBuffer str) {
        StringBuffer aux2 = new StringBuffer(str);
        int a = aux2.indexOf("<script");
        int b = aux2.indexOf("</script>",a);
        while ((a != -1) && (b != -1)) {

            if (a < b) {
                aux2 = new StringBuffer(aux2.replace(a,b + 9, " "));
            } else {
                aux2 = new StringBuffer(aux2.replace(a, a + 7, " "));
            }
            a = aux2.indexOf("<script");
            b = aux2.indexOf("</script>",a);
        }
        return aux2;
    }

    private StringBuffer contieneBasuraStyle(StringBuffer str) {
        StringBuffer aux2 = new StringBuffer(str);
        int a = aux2.indexOf("<style");
        int b = aux2.indexOf("</style>",a);
        while ((a != -1) && (b != -1)) {
            if (a < b) {
                aux2 = new StringBuffer(aux2.replace(a, b + 9, " "));
            } else {
                aux2 = new StringBuffer(aux2.replace(a, a + 7, " "));
            }
            a = aux2.indexOf("<style");
            b = aux2.indexOf("</style>",a);
        }
        return aux2;
    }

    private StringBuffer contieneBasuraA(StringBuffer str) {
        StringBuffer aux2 = new StringBuffer(str);
        int a = aux2.indexOf("<a");
        int b = aux2.indexOf("</a>",a);
        while ((a != -1) && (b != -1)) {
            if (a < b) {
                aux2 = new StringBuffer(aux2.replace(a, b + 4, " "));
            } else {
                aux2 = new StringBuffer(aux2.replace(a, a + 2, " "));
            }
            a = aux2.indexOf("<a");
            b = aux2.indexOf("</a>",a);
        }
        return aux2;
    }

    private StringBuffer contieneBasuraLabel(StringBuffer str) {
        StringBuffer aux2 = new StringBuffer(str);
        int a = aux2.indexOf("<label");
        int b = aux2.indexOf("</label>",a);
        while ((a != -1) && (b != -1)) {
            if (a < b) {
                aux2 = new StringBuffer(aux2.replace(a, b + 8, " "));
            } else {
                aux2 = new StringBuffer(aux2.replace(a, a + 6, " "));
            }
            a = aux2.indexOf("<label");
            b = aux2.indexOf("</label>",a);
        }
        return aux2;
    }

    private StringBuffer contieneBasuraDiv(StringBuffer str) {
        StringBuffer aux2 = new StringBuffer(str);
        int a = str.indexOf("<div");
        int b = str.indexOf("</div>",a);
        while ((a != -1) && (b != -1)) {
            if (a < b) {
                aux2 = new StringBuffer(aux2.replace(a, b + 6, " "));
            } else {
                aux2 = new StringBuffer(aux2.replace(a, a + 4, " "));
            }
            a = aux2.indexOf("<div");
            b = aux2.indexOf("</div>",a);
        }
        return aux2;
    }

    private StringBuffer contieneBasuraFinal(StringBuffer str) {
        StringBuffer aux2 = new StringBuffer(str);
        int a = str.indexOf("<");
        int b = str.indexOf(">",a);
        while ((a != -1) && (b != -1)) {
            if (a < b) {
                aux2 = new StringBuffer(aux2.replace(a, b + 1, " "));
            } else {
                aux2 = new StringBuffer(aux2.replace(a, b + 1, " "));
            }
            a = aux2.indexOf("<");
            b = aux2.indexOf(">",a);
        }
        return aux2;
    }

    private StringBuffer contieneCopyright(StringBuffer str) {
        StringBuffer sb = new StringBuffer("");
        int a1 = str.indexOf("Todos los derechos reservados");
        int a2 = str.indexOf("Reservados todos los derechos");
        int b = str.indexOf("Tots els drets reservats");
        int c = str.indexOf("All rights reserved");
        if (a1 != -1 || a2 != -1 || b != -1 || c != -1) {
            return sb;
        } else {
            return str;
        }
    }

    private String tokenizarCarext(String st) {
        String[] carext = {"'","&pound;", "&rsaquo;", "&quot;", "&ldquo;", "&rdquo;", "&acute;", "&aacute;", "&eacute;", "&iacute;", "&oacute;", "&uacute;", "&Aacute;", "&Eacute;", "&Iacute;", "&Oacute;", "&Uacute;", "&agrave;", "&egrave;", "&igrave;", "&ograve;", "&ugrave;", "&Agrave;", "&Egrave;", "&Igrave;", "&Ograve;", "&Ugrave;", "&uml;", "&auml;", "&euml;", "&iuml;", "&ouml;", "&uuml;", "&Auml;", "&Euml;", "&Iuml;", "&Ouml;", "&Uuml;", "&amp;", "&lt;", "&gt;", "&iexcl;", "&iquest;", "&nbsp;",  "&ccedil;", "&Ccedil;", "&ntilde;", "&Ntilde;"};
        String[] carlat = {  "\u0027","",       "",        "",        "",       "",        "\u0060",  "\u00E1",  "\u00E9",    "\u00ED",   "\u00F3",   "\u00FA",   "\u00C1",  "\u00C9",   "\u00CD",  "\u00D3",    "\u00DA",   "\u00E0",   "\u00E8",   "\u00EC",   "\u00F2",   "\u00F9",   "\u00C0",   "\u00C8",   "\u00CC",   "\u00D2",   "\u00D9", "\u00A8","\u00E4","\u00EB", "\u00EF", "\u00F6",  "\u00FC", "\u00C4",  "\u00CB","\u00CF","\u00D6",  "\u00DC", "\u0026","\u003C","\u003E","\u00A1","\u00BF",   "\u00A0",  "\u00E7",   "\u00C7",     "\u00F1",    "\u00D1"};
        for (int i = 0; i < carext.length; i++) {
            st = new String(st.replaceAll(carext[i], carlat[i]));
        }
        return st;
    }

    private String tagMayMin(String st) {
        String[] tagmin = {"<div", "</div>", "<a", "</a>", "<p>", "</p>", "<script", "</script>", "<style", "</style>", "<label", "</label>", "<h1>", "</h1", "<h2>", "</h2", "<h3>", "</h3", "<title>", "</title>"};
        String[] tagmay = {"<DIV", "</DIV>", "<A", "</A>", "<P>", "</P>", "<SCRIPT", "</SCRIPT>", "<STYLE", "</STYLE>", "<LABEL", "</LABEL>", "<H1>", "</H1", "<H2>", "</H2", "<H3>", "</H3", "<TITLE>", "</TITLE>"};
        for (int i = 0; i < tagmay.length; i++) {
            st = new String(st.replaceAll(tagmay[i], tagmin[i]));
        }
        return st;
    }

    private String tokenizarSig(String st) {
        Pattern pn = Pattern.compile("[a-zA-Z]");
        String pal;
        StringTokenizer stok = new StringTokenizer(st);
        int q = stok.countTokens();
        StringBuffer list = new StringBuffer("");
        char[] nacc = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
        char[] acc = {'\u00c0', '\u00c1', '\u00c2', '\u00c4', '\u00c8', '\u00c9', '\u00ca', '\u00cb', '\u00cc', '\u00cd', '\u00ce', '\u00cf', '\u00d2', '\u00d3', '\u00d4', '\u00d6', '\u00d9', '\u00da', '\u00db', '\u00dc', '\u00e0', '\u00e1', '\u00e2', '\u00e4', '\u00e8', '\u00e9', '\u00ea', '\u00eb', '\u00ec', '\u00ed', '\u00ee', '\u00ef', '\u00f2', '\u00f3', '\u00f4', '\u00f6', '\u00f9', '\u00fa', '\u00fb', '\u00fc'};
        for (int i = 0; i < q; i++) {
            pal = stok.nextToken();
            if ((pn.matcher(pal).find())) {
                for (int j = 0; j < acc.length; j++) {
                    pal = pal.replace(acc[j], nacc[j / 4]);
                }
                list.append(pal);
                list.append(" ");
            }
        }
        return list.toString();
    }

    private String quitaTabulador(String str) {
        String st = "";
        if (str.indexOf('\t') != -1) {
            for (int x = 0; x < str.length(); x++) {
                if (str.charAt(x) != '\t') {
                    st += str.charAt(x);
                }
            }
        } else {
            st = new String(str);
        }
        return st;
    }

    private String quitaEspacios(String str) {
        String st = "";
        for (int x = 0; x < str.length(); x++) {
            if (str.charAt(x) != ' ') {
                st += str.charAt(x);
            } else {
                x++;
                if (x < str.length()) {
                    if (str.charAt(x) != ' ') {
                        st += ' ';
                        st += str.charAt(x);
                    }
                }
            }
        }
        return st;
    }

    private boolean esNoticia() {
        return (this.titulo.length() != 0 && this.texto.length() != 0);
    }

    public TitTex sacaTexto(StringBuffer str) throws NoticiaNoValidaException {
        TitTex tt = new TitTex();
        try {
        this.crearParseHttp();
        str = new StringBuffer(tagMayMin(str.toString()));
        contieneTitulo(str);
        contieneTextoH1(str);
        contieneTextoH2(str);
        contieneTextoH3(str);
        contieneTextoP(str);
        contieneTextoDiv(str);
        contieneTextoSpan(str);
        }
        catch (Exception e){
            throw new NoticiaNoValidaException("Noticia no v\u00e1lida: "+e.getMessage());
        }
        if (!esNoticia()) {
            throw new NoticiaNoValidaException("Noticia no v\u00e1lida");
        } else {
            tt.titulo = this.titulo.toString();
            tt.texto = this.texto.toString();
        }
        return tt;

    }
}
